package connector;

import cn.dai.pojo2.DruidUtils;
import cn.dai.util.JdbcDruidUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author ArkD42
 * @file Jdbc
 * @create 2020 - 04 - 24 - 21:46
 */
public class DruidTest {

    @Test
    public void dt1() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();

        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///jdbc_db?serverTimezone=Asia/Shanghai");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

    @Test
    public void dt2() throws Exception {
        String path = DruidTest.class.getClassLoader().getResource("druid.properties").getFile();
        String decode = URLDecoder.decode(path, "utf-8");
        FileInputStream inputStream = new FileInputStream(decode);
        Properties properties = new Properties();
        properties.load(inputStream);
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

    @Test
    public void dt3() throws Exception {
        Connection connection = DruidUtils.getConnection();
        System.out.println(connection);
        connection.close();
    }

}
